package com.ruoyi.timesheets.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;


import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.employeeInfomation.domain.SalaryEmployee;
import com.ruoyi.employeeInfomation.mapper.SalaryEmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.timesheets.mapper.EmployeeTimesheetsMapper;
import com.ruoyi.timesheets.domain.EmployeeTimesheets;
import com.ruoyi.timesheets.service.IEmployeeTimesheetsService;

/**
 * 员工考勤表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-14
 */
@Service
public class EmployeeTimesheetsServiceImpl extends ServiceImpl<EmployeeTimesheetsMapper, EmployeeTimesheets> implements IEmployeeTimesheetsService {
    @Autowired
    private EmployeeTimesheetsMapper employeeTimesheetsMapper;
    @Autowired
    private SalaryEmployeeMapper salaryEmployeeMapper;

    /**
     * 查询员工考勤表
     *
     * @param id 员工考勤表主键
     * @return 员工考勤表
     */
    @Override
    public EmployeeTimesheets selectEmployeeTimesheetsById(Long id) {
        return employeeTimesheetsMapper.selectEmployeeTimesheetsById(id);
    }

    /**
     * 查询员工考勤表列表
     *
     * @param employeeTimesheets 员工考勤表
     * @return 员工考勤表
     */
    @Override
    public List<EmployeeTimesheets> selectEmployeeTimesheetsList(EmployeeTimesheets employeeTimesheets) {
        return employeeTimesheetsMapper.selectEmployeeTimesheetsList(employeeTimesheets);
    }

    /**
     * 新增员工考勤表
     *
     * @param employeeTimesheets 员工考勤表
     * @return 结果
     */
    @Override
    public int insertEmployeeTimesheets(EmployeeTimesheets employeeTimesheets) {
            return employeeTimesheetsMapper.insertEmployeeTimesheets(employeeTimesheets);
    }

    /**
     * 修改员工考勤表
     *
     * @param employeeTimesheets 员工考勤表
     * @return 结果
     */
    @Override
    public int updateEmployeeTimesheets(EmployeeTimesheets employeeTimesheets) {
        return employeeTimesheetsMapper.updateEmployeeTimesheets(employeeTimesheets);
    }

    /**
     * 批量删除员工考勤表
     *
     * @param ids 需要删除的员工考勤表主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeTimesheetsByIds(Long[] ids) {
        return employeeTimesheetsMapper.deleteEmployeeTimesheetsByIds(ids);
    }

    /**
     * 删除员工考勤表信息
     *
     * @param id 员工考勤表主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeTimesheetsById(Long id) {
        return employeeTimesheetsMapper.deleteEmployeeTimesheetsById(id);
    }

    @Override
    public int addAttendance() {
        EmployeeTimesheets employeeTimesheets = new EmployeeTimesheets();
        String username = SecurityUtils.getUsername();
        System.out.println("----------------"+username);
        SalaryEmployee salaryEmployee = new SalaryEmployee();
        List<SalaryEmployee> list = salaryEmployeeMapper.selectSalaryEmployeeList(salaryEmployee);
        for (SalaryEmployee emp:list) {
            if(username.equals(emp.getJobNumber())){
                employeeTimesheets.setEmployeeId(emp.getId());
            }
        }
        employeeTimesheets.setClockInTime(new Date());
        employeeTimesheets.setClockOrNot(1);
        return employeeTimesheetsMapper.insertEmployeeTimesheets(employeeTimesheets);
    }
}